package com.neusoft.nepa.controller;

import com.neusoft.nepa.UserLogin;
import com.neusoft.nepa.entity.Admin;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.util.converter.DefaultStringConverter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class AdminManageController {
    @FXML
    private TableColumn<Admin, String> col_phoneNumber;
    @FXML
    private TableColumn<Admin, String> col_userName;
    @FXML
    private TableColumn<Admin, String> col_password;
    @FXML
    private TableColumn<Admin, String> col_remark;
    @FXML
    private TableView<Admin> tableview;
    private final ObservableList<Admin> userList = FXCollections.observableArrayList();
    private UserLogin mainApp;

    @FXML
    public void initialize() {
        // 设置表格列的数据绑定
        col_phoneNumber.setCellValueFactory(new PropertyValueFactory<>("phoneNumber"));
        col_userName.setCellValueFactory(new PropertyValueFactory<>("name"));
        col_password.setCellValueFactory(new PropertyValueFactory<>("password"));
        col_remark.setCellValueFactory(new PropertyValueFactory<>("info"));

        // 设置表格数据
        tableview.setItems(userList);

        // 启用表格编辑功能
        tableview.setEditable(true);

        // 设置用户名和密码列可编辑
        col_userName.setCellFactory(TextFieldTableCell.forTableColumn(new DefaultStringConverter()));
        col_password.setCellFactory(TextFieldTableCell.forTableColumn(new DefaultStringConverter()));
        col_remark.setCellFactory(TextFieldTableCell.forTableColumn(new DefaultStringConverter()));

        // 处理用户名列的编辑提交事件
        col_userName.setOnEditCommit(event -> {
            Admin user = event.getRowValue();
            String newValue = event.getNewValue();
            user.setName(newValue);
            updateUserMapAndSave();
        });

        // 处理密码列的编辑提交事件
        col_password.setOnEditCommit(event -> {
            Admin user = event.getRowValue();
            String newValue = event.getNewValue();
            user.setPassword(newValue);
            updateUserMapAndSave();
        });

        col_remark.setOnEditCommit(event -> {
            Admin user = event.getRowValue();
            String newValue = event.getNewValue();
            user.setInfo(newValue);
            updateUserMapAndSave();
        });
    }

    // 更新用户映射并保存
    private void updateUserMapAndSave() {
        HashMap<String, Admin> userMap = new HashMap<>();
        for (Admin user : userList) {
            userMap.put(user.getName(), user);
        }
        try {
            this.mainApp.resetAccountMap(userMap);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void queryUser() throws IOException {
        Map<String, Admin> userMap = this.mainApp.getUserMap();
        userList.clear();
        ArrayList<String> arrayList = this.mainApp.getAdminKeys();
        for (Admin user : userMap.values()) {
            if (!arrayList.contains(user.getPhoneNumber())) {
                userList.add(user);
            }
        }
    }

    public void setMainApp(UserLogin mainApp) throws IOException {
        this.mainApp = mainApp;
        queryUser();
    }

    @FXML
    private void showUserManageView() throws IOException {
        this.mainApp.showUserManageView();
    }

    @FXML
    private void showDeptManageView() throws IOException {
        this.mainApp.showDeptManageView();
    }

    @FXML
    private void deleteUser() throws IOException {
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("anonchihaya");
        Map<String, Admin> userMap = this.mainApp.deleteUser();
        userList.clear();
        for (Admin user : userMap.values()) {
            if (!arrayList.contains(user.getPhoneNumber())) {
                userList.add(user);
            }
        }
    }
    @FXML
    private void showColorMap() throws IOException {
        this.mainApp.showColorMap();
    }
}